package cn.wen.yaling.controller;

import java.util.*;

import cn.wen.yaling.commons.entity.TypeEntity;
import cn.wen.yaling.commons.vo.*;
import cn.wen.yaling.xo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.wen.yaling.commons.entity.SubjectEntity;
import cn.wen.yaling.xo.service.SubjectService;
import cn.wen.yaling.commons.utils.ResultEntity;



/**
 * 
 *
 * @author FY
 * @email 932043654@qq.com
 * @date 2022-08-13 02:07:37
 */
@RestController
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private ArticleService articleService;

    /**
     * 通过subjectId 获取专题的基本信息
     * @return
     */
    @GetMapping("/web/subject/info")
    public ResultEntity subjectInfo(@RequestParam("subjectId") Integer subjectId) {
        SubjectEntity subject = subjectService.getById(subjectId);
        return ResultEntity.ok().put("data", subject);
    }

    /**
     * 获取页面的模块分类和下面的文章
     * @return
     */
    @GetMapping("/web/subject/type/article-list")
    public ResultEntity typeArticleList(@RequestParam("subjectId") Integer subjectId) {
        List<SubjectVO> subjects = subjectService.getAllArticleBySubjectId(subjectId);
        return ResultEntity.ok().put("data", subjects);
    }


    /*=================================== 后台Api ===================================*/

    @PostMapping("/admin/subject/add")
    public ResultEntity addSubject(@RequestBody SubjectAddVO subjectAddVO) {
        boolean res = subjectService.addSubject(subjectAddVO);
        if (!res) return ResultEntity.error();
        return ResultEntity.ok();
    }

    @PostMapping("/admin/subject/update")
    public ResultEntity updateSubject(@RequestBody SubjectUpdateVO subjectUpdateVO) {
        boolean res = subjectService.updateSubject(subjectUpdateVO);
        if (!res) return ResultEntity.error();
        return ResultEntity.ok();
    }

    /**
     * 获取所有的专题列表
     * @return
     */
    @GetMapping("/admin/subject/list")
    public ResultEntity subjectList() {
        List<SubjectListVO> subjects = subjectService.getAllSubjectList();
        return ResultEntity.ok().put("data", subjects);
    }

    @GetMapping("/admin/subject/search")
    public ResultEntity searchSubject(@RequestParam("subjectName") String subjectName) {
        List<SubjectListVO> resources = subjectService.searchSubject(subjectName);
        return ResultEntity.ok().put("data", resources);
    }

    @GetMapping("/admin/subject/article-list")
    public ResultEntity articleList(@RequestParam("subjectId") Integer subjectId) {
        List<SubjectAdminVO> subjects = subjectService.getAllSimpleArticleInfoBySubjectId(subjectId);
        return ResultEntity.ok().put("data", subjects);
    }

    /**
     * 通过subjectIds 来删除专题列表
     * @return
     */
    @PostMapping("/admin/subject/delete")
    public ResultEntity deleteSubject(@RequestParam("subjectIds") Integer[] subjectIds) {
        boolean res = subjectService.deleteByIds(subjectIds);
        if (!res) return ResultEntity.error();
        return ResultEntity.ok();
    }





}
